package com.ziyueyuwen.xiniao.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.suyeer.cache.RedisUtil;
import com.ziyueyuwen.xiniao.bean.question.PointDO;
import com.ziyueyuwen.xiniao.bean.question.PointSelectBean;
import com.ziyueyuwen.xiniao.bean.question.ZyQuestionPoint;
import com.ziyueyuwen.xiniao.dao.ZyQuestionPointMapper;
import com.ziyueyuwen.xiniao.service.PointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wancheng  on 2019/1/9.
 */
@Service
public class PointServiceImpl implements PointService {

    private final String POINT_TREE_KEY = "POINT_TREE";

    @Autowired
    ZyQuestionPointMapper zyQuestionPointMapper;

    @Override
    public ZyQuestionPoint insert(ZyQuestionPoint point) {
        zyQuestionPointMapper.insertUseGeneratedKeys(point);
        return point;
    }

    @Override
    public ZyQuestionPoint update(ZyQuestionPoint point) {
        zyQuestionPointMapper.updateByPrimaryKeySelective(point);
        point = zyQuestionPointMapper.selectByPrimaryKey(point.getId());
        return point;
    }

    @Override
    public void delete(Integer pointId) {
        zyQuestionPointMapper.deleteByPrimaryKey(pointId);
    }

    @Override
    public List<ZyQuestionPoint> getPoint(Integer parentId) {
        if (parentId == null) {
            parentId = 0;
        }
        ZyQuestionPoint questionPoint = new ZyQuestionPoint();
        questionPoint.setParent(parentId);
        List<ZyQuestionPoint> points = zyQuestionPointMapper.select(questionPoint);
        return points;
    }

    @Override
    public List<PointSelectBean> getPointSelectBean() {
        List<ZyQuestionPoint> points = zyQuestionPointMapper.selectAll();
        List<PointSelectBean> selectBeen = points.stream().map(p -> new PointSelectBean(p.getId(), p.getParent(), p.getLevel(), p.getName(), p.getId().toString())).collect(Collectors.toList());
        for (int i = 0; i < selectBeen.size(); i++) {
            for (int j = 0; j < selectBeen.size(); j++) {
                if (selectBeen.get(j).getParent().intValue() == selectBeen.get(i).getId().intValue()) {
                    selectBeen.get(i).addChildren(selectBeen.get(j));
                }
            }
        }
        Map<Integer, List<PointSelectBean>> levelPoint = selectBeen.stream().collect(Collectors.groupingBy(PointSelectBean::getLevel));
        return levelPoint.get(1);
    }

    @Override
    public Set<Integer> getPointParentIds(Integer pointId) {
        List<ZyQuestionPoint> points = zyQuestionPointMapper.selectAll();
        Map<Integer,ZyQuestionPoint> idPointMap = points.stream().collect(Collectors.toMap(ZyQuestionPoint::getId,p->p));
        Set<Integer> parentIds = new HashSet<>();
        parentIds.add(pointId);
        //上级知识点
        ZyQuestionPoint parent = idPointMap.get(idPointMap.get(pointId).getParent());
        while (parent != null){
            parentIds.add(parent.getId());
            parent = idPointMap.get(parent.getParent());
        }
        return parentIds;
    }

    @Override
    public Set<Integer> getPointParentIds(Set<Integer> pointIds) {
        List<ZyQuestionPoint> points = zyQuestionPointMapper.selectAll();
        Map<Integer,ZyQuestionPoint> idPointMap = points.stream().collect(Collectors.toMap(ZyQuestionPoint::getId,p->p));
        Set<Integer> parentIds = new HashSet<>();
        pointIds.forEach(
                pid->{
                    parentIds.add(pid);
                    //上级知识点
                    ZyQuestionPoint parent = idPointMap.get(idPointMap.get(pid).getParent());
                    while (parent != null){
                        parentIds.add(parent.getId());
                        parent = idPointMap.get(parent.getParent());
                    }
                }
        );
        return parentIds;
    }


}
